﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Microsoft.Research.Kinect.Nui;
using Coding4Fun.Kinect.Wpf;
using BaffoHat.Wpf.ViewModel;

namespace BaffoHat.Wpf.Controls
{
    public class KinectMouse : IMouse
    {
        public event MouseMoveEventHandler MouseMove;

        public KinectMouse()
        {
            if(KinectSensor.Instance.IsInizialized)
                KinectSensor.Instance.Runtime.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(SkeletonFrameReady);
        }

        private void SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            SkeletonData skeleton = (from s in e.SkeletonFrame.Skeletons
                                     where s.TrackingState == SkeletonTrackingState.Tracked
                                     select s).FirstOrDefault();

            if (skeleton == null) return;

            int width =  Convert.ToInt32(System.Windows.SystemParameters.PrimaryScreenWidth);
            int height = Convert.ToInt32(System.Windows.SystemParameters.PrimaryScreenHeight);

            Vector nuiv = skeleton.Joints[JointID.HandRight].ScaleTo(width, height, 0.50f, 0.30f).Position;
            System.Windows.Point point = new System.Windows.Point(nuiv.X, nuiv.Y);

            if (MouseMove != null)
                MouseMove(this, new MouseMoveEventArgs(point));
        }
    }
}
